package com.cetc.nsfx.service.sys;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;





import com.cetc.nsfx.base.BaseService;
//import com.cetc.nsfx.base.BaseService;
import com.cetc.nsfx.entity.SysAssignPrivs;
import com.cetc.nsfx.entity.SysMenu;
import com.cetc.nsfx.entity.SysPrivs;
import com.cetc.nsfx.entity.SysRoles;
import com.cetc.nsfx.mapper.StationMapper;
import com.cetc.nsfx.security.vo.UserContext;
import com.github.miemiedev.mybatis.paginator.domain.PageBounds;
import com.github.miemiedev.mybatis.paginator.domain.PageList;


@Service
@Transactional
public class StationService  extends BaseService{
	
	 @Autowired
	 private StationMapper stationMapper;
	 
	 /**
	  * 岗位列表数据
	  * @param loginName
	  * @param roleName
	  * @param roleCode
	  * @return
	  */
	 public Map<String, Object>  loadRoleList(String loginName,String roleName,int iDisplayStart,int iDisplayLength){
		 
		 Map<String, Object> rs = new HashMap<String,Object>();
		 Map<String, Object> p = new HashMap<String,Object>();
		 
		 p.put("loginName", loginName);
		 p.put("roleName", roleName);
		 
		 PageBounds pageBounds = new PageBounds((iDisplayStart/iDisplayLength)+1,iDisplayLength);
		 List<SysRoles> loadRoleList = this.getSqlSessionTemplate().selectList("com.cetc.nsfx.mapper.StationMapper.getRoleList", p,
				 pageBounds );
		 @SuppressWarnings("unchecked")
		PageList<SysRoles> pageList = (PageList<SysRoles>)loadRoleList;
		 rs.put("rs", pageList);
		 rs.put("count", pageList.getPaginator().getTotalCount());
		 return rs;
		 
		 
		
	 }
	 
	 
	    public List<Map<String,Object>> loadSysRoleModel(String loginName,String roletype){
	    	
	    	Map<String,String> params = new HashMap<String,String>();
	    	params.put("loginName", loginName);
	    	params.put("roletype", roletype);
	    	return stationMapper.getStationList(params);
	    }
	   
	    @Transactional(readOnly = false)
	    public int saveOrUpdate(String roleCode,String roleName,String roleType,String parentCode,String[] menuCode,String roleDesc){
	    	
	    	int result=0;//-1:新增编码已存在 0:保存失败 1：保存成功
	    	UserContext user = (UserContext) SecurityUtils.getSubject().getPrincipal();
	    	SysRoles sysRole = new SysRoles();
			if("".equals(roleCode) || null == roleCode){
				
//				if(!CollectionUtils.isEmpty(list)){//根据角色代码检查是否已经存在
//					return result = -1;
//				}else{
					try{
					
					
//					String sql = "select SEQ_SYS_ROLE.nextval from dual";
					List list = stationMapper.getRoleSeq();
					sysRole.setRoleCode(roleType+"_"+list.get(0).toString());
					sysRole.setRoleName(roleName);
					sysRole.setRoleType(roleType);
					sysRole.setLoginName(user.getUserId());
					sysRole.setParentCode(parentCode);
					sysRole.setRoleDesc(roleDesc);
					stationMapper.saveStationInfo(sysRole);//新增角色
					this.saveOrUpdateRoleMenu(menuCode,sysRole.getRoleCode());
					result = 1;
					}catch(Exception e){
						e.printStackTrace();
					}
			}else{
				try{
					 
					 sysRole.setRoleCode(roleCode);
					stationMapper.delByPrivsCode(roleCode);

					sysRole.setRoleName(roleName);
					sysRole.setRoleType(roleType);
					sysRole.setParentCode(parentCode);
					sysRole.setRoleDesc(roleDesc);

					sysRole.setLoginName(user.getUserId());
					

					stationMapper.updStationInfo(sysRole);//更新角色信息

					this.saveOrUpdateRoleMenu(menuCode,roleCode);
					result = 1;
				}catch(Exception e){
					e.printStackTrace();
				}
			}
			
			return result;
	    }
	    
	    @Transactional(readOnly = false)
	    public int delete(String roleCode){
	    	int result=0;// 0:删除失败 1：删除成功
	    	try{
	    		stationMapper.delByPrivsCode(roleCode);
	    		stationMapper.delByRoleCode(roleCode);
		    	result=1;
	    	}catch(Exception e){
				e.printStackTrace();
			}
	    	return result;
	    }
	    
	    @Transactional(readOnly = false)
	    public int saveOrUpdateRoleMenu(String[] menuCode,String roleCode){
	    	int result=0;// 0:保存失败 1：保存成功
//	    	String [] menu = menuID.split("#");
	    	try{
	    		/*驾驶舱指标权限设置--修改前的权限*/
	    		List<SysMenu> sysMenu = null;
	    		Map<String,List<SysPrivs>> privsMap = null;
	    		
	    		/*end*/
	    		stationMapper.delByPrivsCode(roleCode);
		    	List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
		    	

		    	//menu ['30002','30001']
		    	if(menuCode.length > 0){
			    	

			    	for(int i = 0 ;i<menuCode.length;i++){//全选状态菜单数据
			    		//String []val = rigthMenu[i].replace("|", "&").split("&");
				    	SysPrivs sysPrivs = new SysPrivs();
				    	sysPrivs.setRoleCode(roleCode); //岗位ID
				    	sysPrivs.setResourceType("1");
				    	sysPrivs.setResourceCode(menuCode[i]); //菜单ID
				    	sysPrivs.setVview("1");
					    	sysPrivs.setImports("0");
					    	sysPrivs.setExportExcel("0");
					    	sysPrivs.setExportCsv("0");
					    	sysPrivs.setExportPdf("0");
					    	sysPrivs.setReport("0");
					    	sysPrivs.setPresPriv1("0");
					    	sysPrivs.setPresPriv2("0");
					    	sysPrivs.setPresPriv3("0");
					    	sysPrivs.setSelectAllNode("1");
					    	sysPrivs.setReportView("0");
//				    	if("1".equals(val[1])){
//				    		sysPrivs.setSelectAllNode("1");
//				    	}
				    	    list.add(sysPrivs.toMap());
			    	}
		    	}
		    	

//		    	if(menuCode.length > 1){
//		    		String []rigthFolder = menuCode[1].split(",");
//			    	for(int i = 0 ;i<rigthFolder.length;i++){//半选状态菜单数据
//			    		SysPrivs sysPrivs = new SysPrivs();
//				    	sysPrivs.setRoleCode(roleCode);
//				    	sysPrivs.setResourceType("1");
//				    	sysPrivs.setVview("1");
//				    		sysPrivs.setResourceCode(rigthFolder[i]);
//					    	sysPrivs.setImports("0");
//					    	sysPrivs.setExportExcel("0");
//					    	sysPrivs.setExportCsv("0");
//					    	sysPrivs.setExportPdf("0");
//					    	sysPrivs.setReport("0");
//					    	sysPrivs.setPresPriv1("0");
//					    	sysPrivs.setPresPriv2("0");
//					    	sysPrivs.setPresPriv3("0");
//				    	sysPrivs.setSelectAllNode("0");
//				    	list.add(sysPrivs);
//			    	}
//		    	}

		    	if(list.size() > 0){
		    		stationMapper.savePrivs(list);
		    	}
		    	
		    	/*end*/
		    	result=1;
	    	}catch(Exception e){
				e.printStackTrace();
	    	}
	    	return result;
	    }

	    /**
	     * 系统菜单查询
	     * @param param
	     * @return
	     */
	    public List<SysMenu> getSysMenu(Map<String,Object> param){
			return stationMapper.getSysMenu(param);
		}
	    
	    /**
	     * 根据登陆用户名获取当前用户所有的角色信息
	     * @param principalName
	     * @return
	     */
	    public List<SysRoles> getSysRoleByPrincipalName(String principalName){
	    	return stationMapper.getSysRoleByPrincipalName(principalName);
	    }
	    /**
	     * 根据登陆用户名获取当前用户所有的角色菜单信息
	     * @param principalName
	     * @return
	     */
	    public List<SysAssignPrivs> getSysAssignPrivsByPrincipalName(String principalName){
	    	return stationMapper.getSysAssignPrivsByPrincipalName(principalName);
	    }
	    
	    /**
	     * 根据岗位编码查询菜单名称
	     * @param roleCode
	     * @return
	     */
	    public List<Map<String,Object>> getRoleMenuName(String roleCode){
	    	return stationMapper.getRoleMenuName(roleCode);
	    }
	 
}
